package com.maxuxu.springsecurity.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    // 链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 首页所有人可以访问，功能页有权限的人访问
        http.authorizeRequests()
                // 所有人都可以访问首页
                .antMatchers("/").permitAll()
                // 必须vip1才可以
                .antMatchers("/level1/**").hasRole("vip1");

        // 没有权限默认会到登陆页面，需要开启登录的页面
        http.formLogin();

        // 注销，开启注销功能,跳到首页
        http.logout().logoutSuccessUrl("/");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 本机内存验证
        auth.inMemoryAuthentication()
                .withUser("maxuxu").password("12345").roles("vip1","vip2")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("12345")).roles("vip1")
                 .and()
                .withUser("ma").password("12345");
    }
}
